package proxy.wrappers;

import android.os.Bundle;
import android.os.IBinder;
import androidx.core.app.NotificationCompat;
import com.genymobile.scrcpy.Ln;
import com.genymobile.scrcpy.SettingsException;
import java.io.Closeable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/* loaded from: classes2.dex */
public class ContentProvider implements Closeable {
    private static final String CALL_METHOD_GET_GLOBAL = "GET_global";
    private static final String CALL_METHOD_GET_SECURE = "GET_secure";
    private static final String CALL_METHOD_GET_SYSTEM = "GET_system";
    private static final String CALL_METHOD_PUT_GLOBAL = "PUT_global";
    private static final String CALL_METHOD_PUT_SECURE = "PUT_secure";
    private static final String CALL_METHOD_PUT_SYSTEM = "PUT_system";
    private static final String CALL_METHOD_USER_KEY = "_user";
    private static final String NAME_VALUE_TABLE_VALUE = "value";
    public static final String TABLE_GLOBAL = "global";
    public static final String TABLE_SECURE = "secure";
    public static final String TABLE_SYSTEM = "system";
    private Object attributionSource;
    private Method callMethod;
    private int callMethodVersion;
    private final ActivityManager manager;
    private final String name;
    private final Object provider;
    private final IBinder token;

    ContentProvider(ActivityManager manager, Object provider, String name, IBinder token) {
        this.manager = manager;
        this.provider = provider;
        this.name = name;
        this.token = token;
    }

    private Method getCallMethod() throws NoSuchMethodException {
        if (this.callMethod == null) {
            try {
                Class<?> attributionSourceClass = Class.forName("android.content.AttributionSource");
                this.callMethod = this.provider.getClass().getMethod(NotificationCompat.CATEGORY_CALL, attributionSourceClass, String.class, String.class, String.class, Bundle.class);
                this.callMethodVersion = 0;
            } catch (ClassNotFoundException | NoSuchMethodException e) {
                try {
                    this.callMethod = this.provider.getClass().getMethod(NotificationCompat.CATEGORY_CALL, String.class, String.class, String.class, String.class, String.class, Bundle.class);
                    this.callMethodVersion = 1;
                } catch (NoSuchMethodException e2) {
                    try {
                        this.callMethod = this.provider.getClass().getMethod(NotificationCompat.CATEGORY_CALL, String.class, String.class, String.class, String.class, Bundle.class);
                        this.callMethodVersion = 2;
                    } catch (NoSuchMethodException e3) {
                        this.callMethod = this.provider.getClass().getMethod(NotificationCompat.CATEGORY_CALL, String.class, String.class, String.class, Bundle.class);
                        this.callMethodVersion = 3;
                    }
                }
            }
        }
        return this.callMethod;
    }

    private Object getAttributionSource() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        if (this.attributionSource == null) {
            Class<?> cl = Class.forName("android.content.AttributionSource$Builder");
            Object builder = cl.getConstructor(Integer.TYPE).newInstance(0);
            cl.getDeclaredMethod("setPackageName", String.class).invoke(builder, "com.android.shell");
            this.attributionSource = cl.getDeclaredMethod("build", new Class[0]).invoke(builder, new Object[0]);
        }
        return this.attributionSource;
    }

    private Bundle call(String callMethod, String arg, Bundle extras) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Object[] args;
        try {
            Method method = getCallMethod();
            switch (this.callMethodVersion) {
                case 0:
                    args = new Object[]{getAttributionSource(), "settings", callMethod, arg, extras};
                    break;
                case 1:
                    args = new Object[]{"com.android.shell", null, "settings", callMethod, arg, extras};
                    break;
                case 2:
                    args = new Object[]{"com.android.shell", "settings", callMethod, arg, extras};
                    break;
                default:
                    args = new Object[4];
                    args[0] = "com.android.shell";
                    args[1] = callMethod;
                    args[2] = arg;
                    args[3] = extras;
                    break;
            }
            return (Bundle) method.invoke(this.provider, args);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
            throw e;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        this.manager.removeContentProviderExternal(this.name, this.token);
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    private static String getGetMethod(String table) {
        char c;
        switch (table.hashCode()) {
            case -1243020381:
                if (table.equals(TABLE_GLOBAL)) {
                    c = 2;
                    break;
                }
                c = 65535;
                break;
            case -906273929:
                if (table.equals(TABLE_SECURE)) {
                    c = 0;
                    break;
                }
                c = 65535;
                break;
            case -887328209:
                if (table.equals(TABLE_SYSTEM)) {
                    c = 1;
                    break;
                }
                c = 65535;
                break;
            default:
                c = 65535;
                break;
        }
        switch (c) {
            case 0:
                return CALL_METHOD_GET_SECURE;
            case 1:
                return CALL_METHOD_GET_SYSTEM;
            case 2:
                return CALL_METHOD_GET_GLOBAL;
            default:
                throw new IllegalArgumentException("Invalid table: " + table);
        }
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    private static String getPutMethod(String table) {
        char c;
        switch (table.hashCode()) {
            case -1243020381:
                if (table.equals(TABLE_GLOBAL)) {
                    c = 2;
                    break;
                }
                c = 65535;
                break;
            case -906273929:
                if (table.equals(TABLE_SECURE)) {
                    c = 0;
                    break;
                }
                c = 65535;
                break;
            case -887328209:
                if (table.equals(TABLE_SYSTEM)) {
                    c = 1;
                    break;
                }
                c = 65535;
                break;
            default:
                c = 65535;
                break;
        }
        switch (c) {
            case 0:
                return CALL_METHOD_PUT_SECURE;
            case 1:
                return CALL_METHOD_PUT_SYSTEM;
            case 2:
                return CALL_METHOD_PUT_GLOBAL;
            default:
                throw new IllegalArgumentException("Invalid table: " + table);
        }
    }

    public String getValue(String table, String key) throws SettingsException {
        String method = getGetMethod(table);
        Bundle arg = new Bundle();
        arg.putInt(CALL_METHOD_USER_KEY, 0);
        try {
            Bundle bundle = call(method, key, arg);
            if (bundle == null) {
                return null;
            }
            return bundle.getString(NAME_VALUE_TABLE_VALUE);
        } catch (Exception e) {
            throw new SettingsException(table, "get", key, null, e);
        }
    }

    public void putValue(String table, String key, String value) throws SettingsException {
        String method = getPutMethod(table);
        Bundle arg = new Bundle();
        arg.putInt(CALL_METHOD_USER_KEY, 0);
        arg.putString(NAME_VALUE_TABLE_VALUE, value);
        try {
            call(method, key, arg);
        } catch (Exception e) {
            throw new SettingsException(table, "put", key, value, e);
        }
    }
}